#!/usr/bin/env bash
# 传统 build     sh build.sh dev application-name
# docker build   sh build.sh docker

#需要编译的application
PROFILE=$1
APP_NAME=$2

if [ -z "$PROFILE" ]; then
  echo "usage bash ${0} {PROFILE}"
  exit 0
fi

#基础模块打包上传本地仓库
mvn clean install -U -DskipTests -P "$1"

# shellcheck disable=SC2236
if [ ! -z "$APP_NAME" ]; then
  echo "指定应用打包-{$APP_NAME}"
  #指定应用打包
  echo mvn clean package -DskipTests -U -P "$PROFILE" -f "$APP_NAME"/pom.xml
  mvn clean package -DskipTests -U -P "$PROFILE" -f "$APP_NAME"/pom.xml
else
  echo "所有应用打包"
  #所有应用打包
  echo maven build all
  applications=("gateway-bootstrap" "gateway-admin-api")
  # shellcheck disable=SC2068
  for application in ${applications[@]}; do
    echo mvn clean package -DskipTests -U -P "$PROFILE" -f "${application}"/pom.xml
    mvn clean package -DskipTests -U -P "$PROFILE" -f "${application}"/pom.xml
  done
fi

# shellcheck disable=SC2006
ROOT_DIR=$(pwd)

OUTPUT_PATH=output_scm

if [[ -d ${OUTPUT_PATH} ]]; then
  rm -rf ${OUTPUT_PATH}
fi

mkdir -p ${OUTPUT_PATH}

modules=("gateway-bootstrap" "gateway-admin-api")
#拷贝tar.gz
# shellcheck disable=SC2068
for module in ${modules[@]}; do
  if [[ -f ${ROOT_DIR}/${module}/target/${module}.tar.gz ]]; then
    cp "${ROOT_DIR}"/"${module}"/target/"${module}".tar.gz "${ROOT_DIR}"/${OUTPUT_PATH}/
  fi
done

# shellcheck disable=SC2164
cd "${ROOT_DIR}"/${OUTPUT_PATH}

# shellcheck disable=SC2068
for module in ${modules[@]}; do
  if [[ -f ${module}.tar.gz ]]; then
    md5sum "${module}".tar.gz >"${module}".tar.gz.md5
  fi
done
